﻿@using Xms.Schema.Abstractions;
@model Xms.Web.Customize.Models.EditEntityModel

<div class="panel panel-default">
    <div class="panel-heading">
        <h3 class="panel-title">
            <a data-toggle="collapse"
               href="#collapseTwo">
                <strong>@app.PrivilegeTree?.LastOrDefault().DisplayName</strong>
            </a>
        </h3>
    </div>
    <div id="collapseTwo" class="panel-collapse collapse in">
        <div class="panel-body">
            <form action="/@app.OrganizationUniqueName/customize/entity/@app.ActionName" method="post" id="editform" class="form-horizontal" role="form">
                @Html.AntiForgeryToken()
                @Html.ValidationSummary()
                @Html.HiddenFor(x => x.SolutionId)
                @Html.HiddenFor(x => x.EntityId)
                <div class="form-group col-sm-12">
                    @Html.LabelFor(x => x.Entity.Name, app.T["name"], new { @class = "col-sm-2 control-label" })
                    <h4 class="col-sm-10 control-text">
                        <span class="form-control-static label label-info">@Model.Entity.Name</span>
                    </h4>
                </div>
                <div class="form-group col-sm-12">
                    @Html.LabelFor(x => x.LocalizedName, app.T["displayname"], new { @class = "col-sm-2 control-label" })
                    <div class="col-sm-10">
                        @Html.TextBoxFor(x => x.LocalizedName, new { @class = "form-control required" })
                    </div>
                </div>
                @if (Model.ParentEntityLocalizedName.IsNotEmpty())
                {
                    <div class="form-group col-sm-12">
                        @Html.LabelFor(x => x.ParentEntityLocalizedName, app.T["entity_parent"], new { @class = "col-sm-2 control-label" })
                        <h4 class="col-sm-10 control-text">
                            <span class="form-control-static label label-info">@Model.ParentEntityLocalizedName</span>
                        </h4>
                    </div>
                }
                <div class="form-group col-sm-12">
                    @Html.LabelFor(x => x.EntityMask, app.T["entity_mask"], new { @class = "col-sm-2 control-label" })
                    @if (Model.IsCustomizable)
                    {
                        <div class="col-sm-10">
                            <label class="checkbox-inline">
                                @Html.RadioButtonFor(x => x.EntityMask, EntityMaskEnum.User, new { @class = "required" }) @app.T["entity_mask_user"]
                            </label>
                            <label class="checkbox-inline">
                                @Html.RadioButtonFor(x => x.EntityMask, EntityMaskEnum.Organization, new { @class = "required" }) @app.T["entity_mask_organization"]
                            </label>
                        </div>
                    }
                    else
                    {
                        <div class="col-sm-10">
                            <label class="checkbox-inline">
                                @Html.RadioButtonFor(x => x.EntityMask, EntityMaskEnum.User, new { @class = "required", @disabled = true }) @app.T["entity_mask_user"]
                            </label>
                            <label class="checkbox-inline">
                                @Html.RadioButtonFor(x => x.EntityMask, EntityMaskEnum.Organization, new { @class = "required", @disabled = true }) @app.T["entity_mask_organization"]
                            </label>
                        </div>
                    }
                </div>
                <div class="form-group col-sm-12">
                    @Html.LabelFor(x => x.LogEnabled, app.T["entity_isloged"], new { @class = "col-sm-2 control-label" })
                    <div class="col-sm-10">
                        <label class="checkbox-inline">
                            @Html.RadioButtonFor(x => x.LogEnabled, true, new { @class = "required" }) @app.T["enabled"]
                        </label>
                        <label class="checkbox-inline">
                            @Html.RadioButtonFor(x => x.LogEnabled, false, new { @class = "required" }) @app.T["disabled"]
                        </label>
                    </div>
                </div>
                <div class="form-group col-sm-12">
                    @Html.LabelFor(x => x.AuthorizationEnabled, app.T["power_control"], new { @class = "col-sm-2 control-label" })
                    <div class="col-sm-10">
                        @if (Model.IsCustomizable)
                        {
                            <label class="checkbox-inline">
                                @Html.RadioButtonFor(x => x.AuthorizationEnabled, true, new { @class = "required" }) @app.T["enabled"]
                            </label>
                            <label class="checkbox-inline">
                                @Html.RadioButtonFor(x => x.AuthorizationEnabled, false, new { @class = "required" }) @app.T["disabled"]
                            </label>
                        }
                        else
                        {
                            <label class="checkbox-inline">
                                @Html.RadioButtonFor(x => x.AuthorizationEnabled, true, new { @class = "required", @disabled = true }) @app.T["enabled"]
                            </label>
                            <label class="checkbox-inline">
                                @Html.RadioButtonFor(x => x.AuthorizationEnabled, false, new { @class = "required", @disabled = true }) @app.T["disabled"]
                            </label>
                        }
                    </div>
                </div>
                <div class="form-group col-sm-12">
                    @Html.LabelFor(x => x.DuplicateEnabled, app.T["repeat_duplicate"], new { @class = "col-sm-2 control-label" })
                    <div class="col-sm-10">
                        @if (Model.IsCustomizable)
                        {
                            <label class="checkbox-inline">
                                @Html.RadioButtonFor(x => x.DuplicateEnabled, true, new { @class = "required" }) @app.T["enabled"]
                            </label>
                            <label class="checkbox-inline">
                                @Html.RadioButtonFor(x => x.DuplicateEnabled, false, new { @class = "required" }) @app.T["disabled"]
                            </label>
                        }
                        else
                        {
                            <label class="checkbox-inline">
                                @Html.RadioButtonFor(x => x.DuplicateEnabled, true, new { @class = "required", @disabled = true }) @app.T["enabled"]
                            </label>
                            <label class="checkbox-inline">
                                @Html.RadioButtonFor(x => x.DuplicateEnabled, false, new { @class = "required", @disabled = true }) @app.T["disabled"]
                            </label>
                        }
                    </div>
                </div>
                <div class="form-group col-sm-12">
                    @Html.LabelFor(x => x.WorkFlowEnabled, app.T["entity_workflowenabled"], new { @class = "col-sm-2 control-label" })
                    <div class="col-sm-10">
                        @if (Model.IsCustomizable)
                        {
                            <label class="checkbox-inline">
                                @Html.RadioButtonFor(x => x.WorkFlowEnabled, true, new { @class = "required" }) @app.T["enabled"]
                            </label>
                            <label class="checkbox-inline">
                                @Html.RadioButtonFor(x => x.WorkFlowEnabled, false, new { @class = "required" }) @app.T["disabled"]
                            </label>
                        }
                        else
                        {
                            <label class="checkbox-inline">
                                @Html.RadioButtonFor(x => x.WorkFlowEnabled, true, new { @class = "required", @disabled = true }) @app.T["enabled"]
                            </label>
                            <label class="checkbox-inline">
                                @Html.RadioButtonFor(x => x.WorkFlowEnabled, false, new { @class = "required", @disabled = true }) @app.T["disabled"]
                            </label>
                        }
                    </div>
                </div>
                <div class="form-group col-sm-12">
                    @Html.LabelFor(x => x.BusinessFlowEnabled, app.T["entity_businessflowenabled"], new { @class = "col-sm-2 control-label" })
                    <div class="col-sm-10">
                        @if (Model.IsCustomizable)
                        {
                            <label class="checkbox-inline">
                                @Html.RadioButtonFor(x => x.BusinessFlowEnabled, true, new { @class = "required" }) @app.T["enabled"]
                            </label>
                            <label class="checkbox-inline">
                                @Html.RadioButtonFor(x => x.BusinessFlowEnabled, false, new { @class = "required" }) @app.T["disabled"]
                            </label>
                        }
                        else
                        {
                            <label class="checkbox-inline">
                                @Html.RadioButtonFor(x => x.BusinessFlowEnabled, true, new { @class = "required", @disabled = true }) @app.T["enabled"]
                            </label>
                            <label class="checkbox-inline">
                                @Html.RadioButtonFor(x => x.BusinessFlowEnabled, false, new { @class = "required", @disabled = true }) @app.T["disabled"]
                            </label>
                        }
                    </div>
                </div>
                <div class="form-group col-sm-12">
                    <label class="col-sm-2 control-label">分组</label>
                    <div class="col-sm-10 required" id="entitygroups">
                    </div>
                </div>
                <div class="form-group col-sm-12">
                    @Html.LabelFor(x => x.Description, "描述", new { @class = "col-sm-2 control-label" })
                    <div class="col-sm-10">
                        @Html.TextAreaFor(x => x.Description, new { @class = "form-control", @row = 5 })
                    </div>
                </div>
                <div class="form-group col-sm-12 text-center" id="form-buttons">
                    <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-saved"></span> 保存</button>
                    <button type="reset" class="btn btn-default"><span class="glyphicon glyphicon-refresh"></span> 重置</button>
                    <button type="button" class="btn btn-warning" id="delBtn" onclick="delEntity()"><span class="glyphicon glyphicon-trash"></span> 删除</button>
                </div>
            </form>
        </div>
    </div>
</div>
@section Scripts {
    <script src="/content/js/jquery.form.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/jquery-validate/jquery.validate.min.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/jquery-validate/localization/messages_zh.min.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script>
        var entityGroups = @(Html.Raw(Model.EntityGroupId.NotEmpty() ? Model.EntityGroupId.SerializeToJson() : "[]"));
        $(function () {
            //表单验证
            Xms.Web.Form($("#editform"), function (response) {
                if (response.IsSuccess) {
                    Xms.Web.Toast(response.Content, true);
                    Xms.Web.Event.publish('refreshtree');
                    return;
                }
                Xms.Web.Alert(false, response.Content);
            });
            bindEntityGroups();
        });
        function bindEntityGroups() {
            Xms.Ajax.GetJson('/api/data/retrieve/all/entitygroup/name/name:asc', null, function (response) {
                    var data = Xms.Web.GetAjaxResult(response);
                    var $container = $('#entitygroups');
                    $(data.content).each(function (i, n) {
                        var isChecked = $.inArray(n.entitygroupid, entityGroups) >= 0;
                        $container.append('<label class="col-sm-3 col-xs-3"><input type="checkbox" name="entitygroupid" value="'+n.entitygroupid+'" '+(isChecked ? 'checked':'')+' />'+n.name+'</label>');
                    });
                });
        }
        function delEntity() {
            Xms.Web.Del($('#EntityId').val(), 'customize/entity/deleteentity', false, function (response) {
                if (response.IsSuccess) {
                    Xms.Web.Event.publish('refreshtree');
                    location.reload();
                    return;
                }
                Xms.Web.Alert(false, response.Content);
            }, null, '@app.T["confirm_delete"]', true, {

                content: '<div class="form-group"><h3 style="color:red;"><input class="" id="anddeltable" type="checkbox" value="true" checked="checked" />同时删除表？</h3></div>',
                checkOk: function (opts) {
                    var ckb = $('#anddeltable').prop('checked');
                    opts.postdata = {
                        DeleteTable: ckb
                    }

                    return true;
                }
            });
        }
    </script>
}